package parser

import (
	"crawler/engine"
	"regexp"
	"strings"
)

// const cityListRe = `<a href="([^><"]+www\.zhenai\.com/zhenghun/[0-9a-z]+)"[^>]*>([^<]+)</a>`
const cityListRe = `<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>([^<]*)</a>`

// 城市列表解析器
func ParseCityList(contents []byte, _ string) engine.ParserResult {
	re := regexp.MustCompile(cityListRe)        //MustCompile：知道一定会匹配
	matches := re.FindAllSubmatch(contents, -1) //Submatch 拆分匹配各部分
	result := engine.ParserResult{}
	//limit := 10
	for _, match := range matches {
		//result.Items = append(result.Items, "City "+string(match[1]))
		//log.Printf("Got Item: %v", "City "+string(match[1]))

		result.Requests = append(result.Requests, engine.Request{
			Url: strings.Replace(string(match[1]), `\u002F`, "/", -1),
			//Url:        string(match[1]),
			ParserFunc: ParseCity,
		})

		// limit--
		// if limit == 0 {
		// 	break
		// }
	}

	return result
}
